package cn.project.engineering.http

import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.os.Build
import androidx.annotation.RequiresApi
import cn.project.engineering.constant.LiveEventBusConstant
import cn.project.engineering.service.LoopJobService
import com.jeremyliao.liveeventbus.LiveEventBus
import log.KLog


@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class NetworkCallbackImpl : ConnectivityManager.NetworkCallback() {
    companion object {
        private const val TAG = "NetworkCallbackImpl"
    }
    override fun onAvailable(network: Network) {
        super.onAvailable(network)

        // 开启屏幕
        LiveEventBus.get(LiveEventBusConstant.INTERNET).post("1")
        KLog.d(TAG, "网络已链接")
    }

    override fun onLost(network: Network) {
        super.onLost(network)
        KLog.d(TAG, "网络已断开")
        LiveEventBus.get(LiveEventBusConstant.INTERNET).post("0")

    }

    override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
        super.onCapabilitiesChanged(network, networkCapabilities)
        if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {

            if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                KLog.d(TAG,"wifi已经连接")
            } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
               KLog.d(TAG,"数据流量已经连接")
            } else {
                KLog.d(TAG,"其他网络")
            }
        }
    }
}